package com.myh.gulimall.product.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import com.myh.common.valid.AddGroup;
import com.myh.common.valid.UpdateGroup;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.myh.gulimall.product.entity.BrandEntity;
import com.myh.gulimall.product.service.BrandService;
import com.myh.common.utils.PageUtils;
import com.myh.common.utils.R;

import javax.validation.Valid;


/**
 * 品牌
 *
 * @author myh
 * @email 2722407149@qq.com
 * @date 2024-01-01 14:39:39
 */
@Api(tags = "品牌管理")
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @ApiOperation("品牌管理模块下的分页查询功能")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "分页参数", required = true)
    })
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId){
		BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @ApiOperation("保存品牌信息")
    @ApiImplicitParams(
            @ApiImplicitParam(name = "brand", value = "品牌实体类", required = true, paramType = "body")
    )
    //@RequiresPermissions("product:brand:save")
    public R save(@Validated(value = AddGroup.class) @RequestBody BrandEntity brand){
//        //因为JSR303中的@Pattern只能用于字符串，所以在代码当中书写关于显示状态的参数校验
//        if (brand.getShowStatus() != null){
//            if (!(brand.getShowStatus() == 0 || brand.getShowStatus() == 1)){
//                return R.error(400,"显示状态为0或1");
//            }
//        }
//        if (result.hasErrors()){
//            Map<String,String> map = new HashMap<>();
//            //1、获取校验的错误结果
//            result.getFieldErrors().forEach((item)->{
//                //获取到的错误提示
//                String message = item.getDefaultMessage();
//                //获取错误的属性名字
//                String field = item.getField();
//                map.put(field,message);
//            });
//            return R.error(400,"提交数据不合法").put("data",map);
//        }else {
//            brandService.save(brand);
//            return R.ok();
//        }
        brandService.save(brand);
        return R.ok();

    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated(value = UpdateGroup.class) @RequestBody BrandEntity brand){
		brandService.updateDetails(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds){
		brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
